#include "amplifier.h"
#include <QPixmap>
#include <QPainter>
#include <QDesktopWidget>
#include <QApplication>


Amplifier::Amplifier(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint);
    setMouseTracking(true);

    m_imageheight = 150;
    setFixedSize(220,220);
    m_sizeinfo = " ";
    hide();
}

void Amplifier::setSizeInfo(QString str){
    m_sizeinfo = str;
}

void Amplifier::setLocation(qreal x, qreal y){
    m_cursorPoint = QPoint(x, y);
    raise();
    int dest_x = x + 4;
    int dest_y = y + 26;

    /// 超出屏幕检测
    const QSize& parent_size = parentWidget()->size();
    if (dest_y + height() > parent_size.height()) {
        dest_y = y - 26 - height();
    }
    if (dest_x + width() > parent_size.width()) {
        dest_x = x - 4 - width();
    }

    move(dest_x, dest_y);
}

void Amplifier::setOriginPixmap(QPixmap originPainting){
    m_originPixmap = originPainting;
}

void Amplifier::paintEvent(QPaintEvent *){

    QPainter painter(this);
    painter.fillRect(rect(), QColor(0,0,0,160));

    QPixmap endPointImage;
    const QSize& parent_size = parentWidget()->size();
    int ratio = QApplication::desktop()->devicePixelRatio();
    if ((m_cursorPoint.x() + 15 < parent_size.width() && m_cursorPoint.x() - 15 > 0)
      && (m_cursorPoint.y() + 11 < parent_size.height() && m_cursorPoint.y() - 11 > 0)) {
        endPointImage = m_originPixmap.
                copy(QRect((m_cursorPoint.x() - 15)*ratio,
                     (m_cursorPoint.y() - 11)*ratio, 30*ratio, 22*ratio))
                .scaled(width()*ratio, m_imageheight*ratio);
        painter.drawPixmap(0,0, endPointImage);
    }
    else {
        endPointImage = m_originPixmap.
                copy(QRect((m_cursorPoint.x() - 15)*ratio,
                     (m_cursorPoint.y() - 11)*ratio, 30*ratio, 22*ratio));
    }

    /// 绘制十字
    painter.setPen(QPen(QColor(0, 180, 255 , 180), 4));
    // 竖线;
    painter.drawLine(QPoint(width() >> 1, 0),
                     QPoint(width() >> 1,
                            m_imageheight - 4));
    // 横线;
    painter.drawLine(QPoint(0, m_imageheight >> 1),
                     QPoint(width(),
                            m_imageheight >> 1));

    /// 绘制放大图的内边框
    painter.setPen(QPen(Qt::white, 2));
    painter.drawRect(2,2,width()-4, m_imageheight-4);

    /// 绘制外边框
    painter.setPen(QPen(Qt::black, 1));
    painter.drawRect(0,0,width()-1,height()-1);


    /// 当前鼠标像素值的RGB信息
    QImage image = m_originPixmap.toImage();
    QColor cursor_pixel = image.pixel(QPoint(m_cursorPoint.x()*ratio,m_cursorPoint.y()*ratio));
    QString select_pt_rgb = QStringLiteral("RGB:(%1,%2,%3)")
                                    .arg(cursor_pixel.red())
                                    .arg(cursor_pixel.green())
                                    .arg(cursor_pixel.blue());


    painter.setPen(Qt::white);
    painter.drawText(QPoint(3, m_imageheight + 24),m_sizeinfo);
    painter.drawText(QPoint(3, m_imageheight + 50),select_pt_rgb);
}
